package com.wkbb.sports.service.impl;

import com.wkbb.common.dto.ResultDTO;
import com.wkbb.sports.mapper.HaokangMapper;
import com.wkbb.sports.model.*;
import com.wkbb.sports.service.HaokangService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Slf4j
public class HaokangServiceImpl implements HaokangService {

    @Autowired
    private HaokangMapper haokangMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDTO putExamineReport(HkBody data) throws Exception {
        if (data.getMeasureId() == null) return ResultDTO.errorResult("参数异常");
        data.initData();
        haokangMapper.insertHkBodyReport(data);
        if (data.getHealthInfo() != null) haokangMapper.insertHkHealthInfo(data.getHealthInfo());
        if (data.getPhysicalData() != null) haokangMapper.insertHkPhysicalData(data.getPhysicalData());
        if (data.getResultComment() != null) haokangMapper.insertHkResultComment(data.getResultComment());
        if (data.getBodyComposition() != null) haokangMapper.insertHkBodyComposition(data.getBodyComposition());
        return ResultDTO.successResult();
    }

    @Override
    @Transactional(readOnly=true)
    public ResultDTO<HkBody> getExamineReport(Map<String, Object> params) throws Exception {
        HkBody hkBody = haokangMapper.selectHkBodyReport(params);
        hkBody.setHealthInfo(haokangMapper.selectHkHealthInfo(params));
        hkBody.setPhysicalData(haokangMapper.selectHkPhysicalData(params));
        hkBody.setResultComment(haokangMapper.selectHkResultComment(params));
        hkBody.setBodyComposition(haokangMapper.selectHkBodyComposition(params));
        return ResultDTO.successResult(hkBody);
    }

}
